package com.api.main.sentinel;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.stereotype.Service;

@Service
public class SentinelDemo {

    @SentinelResource(value = "HelloWorld")
    public void execute() {
        execute("123456789");
        System.out.println("hello world 123");
    }

    public void execute(String key) {
        Entry entry = null;
        try {
            entry = SphU.entry(key);
        } catch (BlockException e) {
            System.out.println("被限流了");
        } finally {
            if (null != entry) {
                entry.exit();
            }
        }
    }
}
